/**
 * API使用示例
 * 展示如何在Vue组件中使用API接口
 */

import { orderApi, teacherApi, deliveryApi, requirementApi } from './index'
import {
  TUTORING_ITEM,
  STUDY_STAGE,
  STUDY_PLACE,
  TEACHER_LEVEL,
  TEACHER_ABROAD,
  STUDENT_ORDER_STATUS,
  TEACHER_DELIVERY_STATUS,
  GOOD_AT_ITEMS,
  AUDIT_STATUS,
  PICTURE_TYPE,
  getTextByValue,
  TUTORING_ITEM_TEXT,
  GOOD_AT_ITEMS_TEXT,
  AUDIT_STATUS_TEXT
} from './constants'

// 示例：在Vue组件中使用API
export const apiUsageExamples = {
  
  // 1. 获取学生端订单列表
  async getStudentOrders() {
    try {
      const params = {
        current: 1,
        size: 10,
        orderStatus: STUDENT_ORDER_STATUS.WAITING_ACCEPT
      }
      const response = await orderApi.getStudentOrderList(params)
      
      if (response.code === '1000') {
        console.log('订单列表:', response.result)
        return response.result
      } else {
        console.error('获取订单列表失败:', response.message)
        return []
      }
    } catch (error) {
      console.error('API调用失败:', error)
      return []
    }
  },

  // 2. 获取订单详情
  async getOrderDetail(orderId) {
    try {
      const response = await orderApi.getStudentOrderDetail(orderId)
      
      if (response.code === '1000') {
        const orderDetail = response.result
        
        // 使用常量转换显示文本
        const displayData = {
          ...orderDetail,
          tutoringItemText: getTextByValue(TUTORING_ITEM_TEXT, orderDetail.tutoringItem),
          studyStageText: getTextByValue(STUDY_STAGE_TEXT, orderDetail.studyStage),
          studyPlaceText: getTextByValue(STUDY_PLACE_TEXT, orderDetail.studyPlace)
        }
        
        console.log('订单详情:', displayData)
        return displayData
      } else {
        console.error('获取订单详情失败:', response.message)
        return null
      }
    } catch (error) {
      console.error('API调用失败:', error)
      return null
    }
  },

  // 3. 发布需求
  async addRequirement(formData) {
    try {
      const requirementDto = {
        tutoringItem: TUTORING_ITEM.HOMEWORK,
        studyStage: STUDY_STAGE.UNDERGRADUATE,
        studyPlace: STUDY_PLACE.DOMESTIC,
        studySchool: formData.school,
        studyMajor: formData.major,
        budgetMin: formData.budgetMin,
        budgetMax: formData.budgetMax,
        teacherLevel: TEACHER_LEVEL.MASTER,
        teacherAbroad: TEACHER_ABROAD.NO_REQUIREMENT,
        requirementText: formData.description,
        comment: formData.comment || ''
      }

      const response = await requirementApi.addRequirement(requirementDto)

      if (response.code === '200') {
        console.log('需求发布成功:', response.result)
        uni.showToast({
          title: '发布成功',
          icon: 'success'
        })
        return true
      } else {
        console.error('需求发布失败:', response.message)
        uni.showToast({
          title: response.message || '发布失败',
          icon: 'error'
        })
        return false
      }
    } catch (error) {
      console.error('API调用失败:', error)
      uni.showToast({
        title: '网络错误',
        icon: 'error'
      })
      return false
    }
  },

  // 4. 获取需求列表
  async getRequirementPage(filterParams = {}) {
    try {
      const params = {
        current: 1,
        size: 20,
        ...filterParams
      }

      const response = await requirementApi.getRequirementPage(params)

      if (response.code === '1000') {
        console.log('需求列表:', response.result)
        return response.result
      } else {
        console.error('获取需求列表失败:', response.message)
        return []
      }
    } catch (error) {
      console.error('API调用失败:', error)
      return []
    }
  },

  // 5. 投递记录
  async addDeliveryRecord(requirementId) {
    try {
      const deliveryRecordDto = {
        requirementId
      }

      const response = await deliveryApi.addDeliveryRecord(deliveryRecordDto)

      if (response.code === '200') {
        console.log('投递成功:', response.result)
        uni.showToast({
          title: '投递成功',
          icon: 'success'
        })
        return true
      } else {
        console.error('投递失败:', response.message)
        uni.showToast({
          title: response.message || '投递失败',
          icon: 'error'
        })
        return false
      }
    } catch (error) {
      console.error('API调用失败:', error)
      uni.showToast({
        title: '网络错误',
        icon: 'error'
      })
      return false
    }
  },

  // 6. 老师认证示例
  async submitTeacherVerify(formData) {
    try {
      const teacherDto = {
        avatar: formData.avatar,
        nickname: formData.nickname,
        phone: formData.phone,
        wechatNo: formData.wechatNo,
        educationLevel: TEACHER_LEVEL.MASTER,
        highestEducationSchool: formData.school,
        major: formData.major,
        goodAtItems: [
          GOOD_AT_ITEMS.DOMESTIC_THESIS,
          GOOD_AT_ITEMS.PROFESSIONAL_COURSE
        ],
        experience: formData.experience,
        selfIntroduction: formData.introduction,
        pictureList: [
          {
            url: formData.idCardFront,
            picType: PICTURE_TYPE.ID_CARD_FRONT
          },
          {
            url: formData.idCardBack,
            picType: PICTURE_TYPE.ID_CARD_BACK
          },
          {
            url: formData.educationCert,
            picType: PICTURE_TYPE.EDUCATION_CERT
          }
        ]
      }

      const response = await teacherApi.submitTeacherVerify(teacherDto)

      if (response.code === '1000') {
        console.log('认证提交成功')
        uni.showToast({
          title: '认证提交成功',
          icon: 'success'
        })
        return true
      } else {
        console.error('认证提交失败:', response.message)
        uni.showToast({
          title: response.message || '认证提交失败',
          icon: 'error'
        })
        return false
      }
    } catch (error) {
      console.error('API调用失败:', error)
      uni.showToast({
        title: '网络错误',
        icon: 'error'
      })
      return false
    }
  },

  // 7. 获取认证信息示例
  async getVerifyInfo() {
    try {
      const response = await teacherApi.getVerifyInfo()

      if (response.code === '1000') {
        const verifyInfo = response.result

        // 转换显示文本
        const displayData = {
          ...verifyInfo,
          educationLevelText: getTextByValue(TEACHER_LEVEL_TEXT, verifyInfo.educationLevel),
          auditStatusText: getTextByValue(AUDIT_STATUS_TEXT, verifyInfo.auditStatus),
          goodAtItemsText: verifyInfo.goodAtItems.map(item =>
            getTextByValue(GOOD_AT_ITEMS_TEXT, item)
          )
        }

        console.log('认证信息:', displayData)
        return displayData
      } else {
        console.error('获取认证信息失败:', response.message)
        return null
      }
    } catch (error) {
      console.error('API调用失败:', error)
      return null
    }
  },

  // 8. 在Vue组件中的完整使用示例
  vueComponentExample: {
    data() {
      return {
        orderList: [],
        loading: false,
        pagination: {
          current: 1,
          size: 10,
          total: 0
        }
      }
    },
    
    async mounted() {
      await this.loadOrderList()
    },
    
    methods: {
      async loadOrderList() {
        this.loading = true
        try {
          const params = {
            current: this.pagination.current,
            size: this.pagination.size,
            orderStatus: STUDENT_ORDER_STATUS.WAITING_ACCEPT
          }
          
          const response = await orderApi.getStudentOrderList(params)
          
          if (response.code === '1000') {
            this.orderList = response.result.records || []
            this.pagination.total = response.result.total || 0
          } else {
            uni.showToast({
              title: response.message || '加载失败',
              icon: 'error'
            })
          }
        } catch (error) {
          console.error('加载订单列表失败:', error)
          uni.showToast({
            title: '网络错误',
            icon: 'error'
          })
        } finally {
          this.loading = false
        }
      },
      
      // 分页加载
      async loadMore() {
        if (this.loading) return
        
        this.pagination.current++
        await this.loadOrderList()
      },
      
      // 刷新数据
      async refresh() {
        this.pagination.current = 1
        this.orderList = []
        await this.loadOrderList()
      }
    }
  }
}
